﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using umbraco.cms.businesslogic.media;

namespace Juzzbott.Ulib
{
	public class MediaManager
	{

		#region Get URL

		/// <summary>
		/// Gets the URL to the media item. If the media item does not exist, an empty string is returned.
		/// </summary>
		/// <param name="mediaID">The ID of the media item to get the URL for.</param>
		/// <returns>The URL to the media item, or an empty string if not found.</returns>
		public static string GetMediaUrl(int mediaID)
		{

			// If the media id is < 1, it's invalid, so return
			if (mediaID < 1)
			{
				// return empty string
				return "";
			}

			// Get the media object
			Media mediaFile = new Media(mediaID);

			// If the mediaFile exists, return the URL property
			if (mediaFile != null && mediaFile.getProperty("umbracoFile") != null)
			{
				// return the value of the media url if it's not null, otherwise return empty.
				return (mediaFile.getProperty("umbracoFile").Value != null ? mediaFile.getProperty("umbracoFile").Value.ToString() : "");
			}
			else
			{
				// No media file, return ""
				return "";
			}

		}

		#endregion


	}
}
